package dao;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List;

import vo.Discuss;

public class DiscussDao {
	Connection conn=null;
	PreparedStatement ps=null;
	Statement st=null;
	ResultSet rs=null;	
	String url = "jdbc:mysql://localhost:3306/shi?useUnicode=true&characterEncoding=utf8"; 
    String user = "root"; 
    String password = "root";
		
	Connection getConnection(){
		try {
		    conn = DriverManager.getConnection(url, user, password);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return conn;
	}
	
	void close(Connection conn){
		try {
			conn.close();
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	
	void close(Connection conn,Statement ps){
		try {
			ps.close();
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		this.close(conn);
	}
	
	void close(Connection conn,Statement ps,ResultSet rs){
		try {
			rs.close();
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		this.close(conn, ps);
	}
	
	static{
		try {
			Class.forName("com.mysql.jdbc.Driver");
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	
	public List<Discuss> getDiscuss(String username){
		List<Discuss> list=new ArrayList<Discuss>();
		this.getConnection();
		try {
			ps=conn.prepareStatement("select * from mydiscuss where username ='"+username+"'");
			rs=ps.executeQuery();
			while(rs.next()){
				Discuss discuss=new Discuss();
				discuss.setId(rs.getInt("id"));
				discuss.setName(rs.getString("name"));
				discuss.setUsername(rs.getString("username"));
				discuss.setMoney(rs.getString("money"));
				discuss.setDiscuss(rs.getString("discuss"));
				discuss.setPeople(rs.getString("people"));
				list.add(discuss);
			}
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}	
		return list;
	}
	
	public List<Discuss> getAll(){
		List<Discuss> list=new ArrayList<Discuss>();
		this.getConnection();
		try {
			ps=conn.prepareStatement("select * from mydiscuss");
			rs=ps.executeQuery();
			while(rs.next()){
				Discuss discuss=new Discuss();
				discuss.setId(rs.getInt("id"));
				discuss.setName(rs.getString("name"));
				discuss.setUsername(rs.getString("username"));
				discuss.setMoney(rs.getString("money"));
				discuss.setDiscuss(rs.getString("discuss"));
				discuss.setPeople(rs.getString("people"));
				list.add(discuss);
			}
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}	
		return list;
	}
	
	public void insert(String name,String username,String money,String discuss,String people){ 
		this.getConnection();
		try {		
				ps= conn.prepareStatement("insert into mydiscuss(name,username,money,discuss,people) values(?,?,?,?,?)");
				ps.setString(1, name);
				ps.setString(2, username);
				ps.setString(3, money);
				ps.setString(4, discuss);
				ps.setString(5, people);
				ps.execute();			
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}finally{
			this.close(conn, ps);
		}
	}

	public void delete(int id){
		this.getConnection();
		try {
			ps=conn.prepareStatement("delete from mydiscuss where id="+id+"");
			ps.execute();
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}finally{
			this.close(conn, ps);
		}		
	}

}
